package model.schedule;

import java.sql.ResultSet;

import model.database.Database;
import model.database.User;

public class AdAuctionHandler extends Ad {

	public AdAuctionHandler(ResultSet rs, Database database) {
		super(rs, database);
	}

	public void handle() {
		User winner = database.getHighestBidder(thisId);
		User seller = database.getSeller(adId);

		if (winner == null) {
			notifySellerNoBids(seller);
			database.markAsHandled(adId);
		} else {
			notifyWinner(winner);
			notifySellerWon(seller);
			database.markAsWon(adId, seller.getId(), winner.getId());
		}
	}

	public void notifySellerWon(User user) {
		user.queueEmail("Din auktion \"" + getTitle()
				+ "\" har avslutats. Vinnaren är: " + user.getFullName()
				+ ".", "Auktionen \"" + getTitle() + "\" lyckad");
	}

	public void notifyWinner(User user) {
		user.queueEmail("Du har vunnit auktionen \""
				+ getTitle() + "\".",  "Vunnen auktion \"" + getTitle() + "\"");
	}

	public void notifySellerNoBids(User user) {
		user.queueEmail("Din auktion \"" + getTitle()
				+ "\" har avslutats utan några bud.", "Auktionen \"" + getTitle()
				+ "\" har avslutats.");
	}
}
